using OhioState.Graphics;
using OhioState.Graphics.Proxy;
using OhioState.Math;
using Tao.OpenGl;

namespace OhioState.Graphics.OpenGL
{
    public class DirectionalLightHandler : OpenGLObject, ILight
    {
        private DirectionalLightProxy lightProxy;
        private int currentLightNumber;

        public DirectionalLightHandler(DirectionalLightProxy lightProxy)
            : base(lightProxy)
        {
            this.lightProxy = lightProxy;
        }

        #region ILight Members
        public OSUColor Ambient
        {
            get { return lightProxy.Ambient; }
            set { lightProxy.Ambient = value; }
        }

        public OSUColor Diffuse
        {
            get { return lightProxy.Diffuse; }
            set { lightProxy.Diffuse = value; }
        }

        public Vector4 Position
        {
            get { return lightProxy.Position; }
            set { lightProxy.Position = value; }
        }

        public OSUColor Specular
        {
            get { return lightProxy.Specular; }
            set { lightProxy.Specular = value; }
        }

        public void Enable(IRenderPanel renderPanel)
        {
            this.Enable(renderPanel, 0);
        }

        public void Enable(IRenderPanel renderPanel, int lightNumber)
        {
            currentLightNumber = lightNumber;
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glPushMatrix();
            Gl.glLoadIdentity();
            float[] white = {1,1,1,1};
            Gl.glLightfv(Gl.GL_LIGHT0 + currentLightNumber, Gl.GL_POSITION, Position.getValue());
            Gl.glLightfv(Gl.GL_LIGHT0 + currentLightNumber, Gl.GL_DIFFUSE, white);
            Gl.glLightfv(Gl.GL_LIGHT0 + currentLightNumber, Gl.GL_SPECULAR, white);

            Gl.glPopMatrix();
            Gl.glEnable(Gl.GL_LIGHT0 + currentLightNumber);
        }

        public void Disable(IRenderPanel renderPanel)
        {
            Gl.glDisable(Gl.GL_LIGHT0 + currentLightNumber);
        }
        #endregion
    }
}
